#include "lua_obj.h"

static int lua_bool(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_set_map(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_set_ctrl_map(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_set_selected_btn(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t btn_id = luaL_checkinteger(L, 2);
    lv_btnmatrix_set_selected_btn(a, btn_id);

    return 0;
}

static int lua_lv_btnmatrix_set_btn_ctrl(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t btn_id = luaL_checkinteger(L, 2);

    return 0;
}

static int lua_lv_btnmatrix_clear_btn_ctrl(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t btn_id = luaL_checkinteger(L, 2);

    return 0;
}

static int lua_lv_btnmatrix_set_btn_ctrl_all(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_clear_btn_ctrl_all(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_set_btn_width(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t btn_id = luaL_checkinteger(L, 2);
    uint8_t width = luaL_checkinteger(L, 3);
    lv_btnmatrix_set_btn_width(a, btn_id, width);

    return 0;
}

static int lua_lv_btnmatrix_set_one_checked(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_get_map(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_lv_btnmatrix_get_selected_btn(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t res = lv_btnmatrix_get_selected_btn(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_btnmatrix_get_btn_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t btn_id = luaL_checkinteger(L, 2);
    lv_btnmatrix_get_btn_text(a, btn_id);

    return 0;
}

static int lua_lv_btnmatrix_has_btn_ctrl(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    uint16_t btn_id = luaL_checkinteger(L, 2);

    return 0;
}

static int lua_lv_btnmatrix_get_one_checked(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);

    return 0;
}

static int lua_auto_gc(lua_State *L)
{
    lv_obj_t *lv_obj = *(lv_obj_t **)luaL_checkudata(L, 1, META_BTNMATRIX);
    luaL_argcheck(L, lv_obj != NULL, 1, "invalid user data");
    if (lv_obj)
    {
        lv_obj_del(lv_obj);
    }
    return 0;
}
static const struct luaL_Reg arraylib_m[] = {
    {"bool", lua_bool},
    {"set_map", lua_lv_btnmatrix_set_map},
    {"set_ctrl_map", lua_lv_btnmatrix_set_ctrl_map},
    {"set_selected_btn", lua_lv_btnmatrix_set_selected_btn},
    {"set_btn_ctrl", lua_lv_btnmatrix_set_btn_ctrl},
    {"clear_btn_ctrl", lua_lv_btnmatrix_clear_btn_ctrl},
    {"set_btn_ctrl_all", lua_lv_btnmatrix_set_btn_ctrl_all},
    {"clear_btn_ctrl_all", lua_lv_btnmatrix_clear_btn_ctrl_all},
    {"set_btn_width", lua_lv_btnmatrix_set_btn_width},
    {"set_one_checked", lua_lv_btnmatrix_set_one_checked},
    {"get_map", lua_lv_btnmatrix_get_map},
    {"get_selected_btn", lua_lv_btnmatrix_get_selected_btn},
    {"get_btn_text", lua_lv_btnmatrix_get_btn_text},
    {"has_btn_ctrl", lua_lv_btnmatrix_has_btn_ctrl},
    {"get_one_checked", lua_lv_btnmatrix_get_one_checked},
    LUA_LV_OBJ_RLT,
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(arraylib_m) / sizeof(struct luaL_Reg);
    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\n", arraylib_m[i].name);
    }
    return 1;
}

static const struct luaL_Reg arraylib_f[] = {
    {"help", help},
    {NULL, NULL}};

LUALIB_API int lua_lv_btnmatrix_funinit(lua_State *L)
{
    luaL_newlib(L, arraylib_f);
    luaL_newmetatable(L, META_BTNMATRIX);  /* create metatable for file handles */
    lua_pushvalue(L, -1);            /* push metatable */
    lua_setfield(L, -2, "__index");  /* metatable.__index = metatable */
    luaL_setfuncs(L, arraylib_m, 0); /* add file methods to new metatable */
    lua_pop(L, 1);                   /* pop new metatable */

    return 1;
}